package com.seveneight.baijiesteward.adapter;

import android.app.Activity;
import android.content.Intent;
import android.database.DatabaseUtils;
import android.util.Log;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.bean.BaseEntity;
import com.seveneight.baijiesteward.bean.BranchBean;
import com.seveneight.baijiesteward.bean.CreditBean;
import com.seveneight.baijiesteward.bean.PlanDetailListBean;
import com.seveneight.baijiesteward.http.HttpConfig;
import com.seveneight.baijiesteward.ui.main.activity.PerfectRepaymentActivity;
import com.seveneight.baijiesteward.ui.main.activity.RepaymentScheduleActivity;
import com.seveneight.baijiesteward.ui.main.activity.UpdateCreditCardDateActivity;
import com.seveneight.baijiesteward.utils.DateUtil;
import com.seveneight.baijiesteward.utils.MyListView;
import com.seveneight.baijiesteward.utils.StringUtils;
import com.seveneight.baijiesteward.views.PlanPayTypeDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 还款计划adapter
 * Created by user1 on 2018/5/31.
 */

public class RepaymentAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    List<PlanDetailListBean> listData;
    Activity activity;
    public RepaymentAdapter(Activity activity, List<PlanDetailListBean> listData){
        mInflater = LayoutInflater.from(activity);
        this.activity=activity;
        this.listData=listData;
    }

    @Override
    public int getCount() {
        return null==listData?0:listData.size();
    }
    public void refresh(List<PlanDetailListBean> listData){
        this.listData=listData;
        this.notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return null==listData?0:listData.size();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_re_pay_list_layout, null); //加载布局
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
         }
        PlanDetailListBean bean=listData.get(i);
         holder.item_pay_money.setText("还款"+bean.getAmount());
         holder.item_pay_time.setText(DateUtil.formatDate(bean.getExecuteTime()));
         if (null != bean.getList()){
             SonPayListAdapter mAdapter=new SonPayListAdapter(activity,bean.getList());
             holder.item_pay_list.setAdapter(mAdapter);
             mAdapter.notifyDataSetChanged();
         }

        return convertView;
    }

    public class ViewHolder{
        TextView item_pay_money;
        TextView item_pay_time;
        MyListView item_pay_list;

        ViewHolder(View view){
            item_pay_money=view.findViewById(R.id.item_pay_money);
            item_pay_time=view.findViewById(R.id.item_pay_time);
            item_pay_list=view.findViewById(R.id.item_pay_list);
        }
    };
    public class SonPayListAdapter extends BaseAdapter{
        LayoutInflater mInflater;
        List<PlanDetailListBean> listData;
        Activity activity;
        public SonPayListAdapter(Activity activity, List<PlanDetailListBean> listData){
            mInflater = LayoutInflater.from(activity);
            this.activity=activity;
            this.listData=listData;
        }

        @Override
        public int getCount() {
            return null==listData?0:listData.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            SonViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_son_pay_list_layout, null); //加载布局
                holder=new SonViewHolder(convertView);
                convertView.setTag(holder);
            } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
                holder = (SonViewHolder) convertView.getTag();
            }
            PlanDetailListBean item=listData.get(i);
            holder.son_recharge_amount.setText(item.getAmount());
            String[] tiem=item.getExecuteTime().split("T");
            if (tiem.length==2){
                holder.son_pay_date_time.setText(tiem[0]);
                holder.son_pay_date_hh_ss.setText(tiem[1]);
            }
            holder.product_category_type.setText(StringUtils.getCategoryType(item.getProductCategoryType()));
            SonViewHolder finalHolder = holder;
            holder.product_category_type.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new PlanPayTypeDialog(activity, finalHolder.product_category_type,dataType(),item).show();
                }
            });
            return convertView;
        }
        public class SonViewHolder{
            TextView son_recharge_amount;
            TextView son_pay_date_time;
            TextView son_pay_date_hh_ss;
            TextView product_category_type;

            SonViewHolder(View view){
                son_recharge_amount=view.findViewById(R.id.son_recharge_amount);
                son_pay_date_time=view.findViewById(R.id.son_pay_date_time);
                son_pay_date_hh_ss=view.findViewById(R.id.son_pay_date_hh_ss);
                product_category_type=view.findViewById(R.id.product_category_type);
            }
        }
        public List<BranchBean> dataType(){
            List<BranchBean> data=new ArrayList<>();
            data.add(new BranchBean("02","02","公共缴费"));
            data.add(new BranchBean("03","03","手机充值"));
            data.add(new BranchBean("04","04","综合业务"));
            data.add(new BranchBean("05","05","综合业务"));
            data.add(new BranchBean("06","06","彩票业务"));
            data.add(new BranchBean("07","07","行政教育"));
            data.add(new BranchBean("08","08","线下服务"));
            data.add(new BranchBean("09","09","微信电商"));
            data.add(new BranchBean("10","10","虚拟电商"));
            data.add(new BranchBean("11","11","保险行业"));
            data.add(new BranchBean("12","12","基金行业"));
            data.add(new BranchBean("13","13","电子票务"));
            data.add(new BranchBean("14","14","金融投资"));
            data.add(new BranchBean("15","15","大额支付"));
            data.add(new BranchBean("16","16","其他"));
            data.add(new BranchBean("17","17","旅游机票"));
            data.add(new BranchBean("18","18","畅付D"));
            data.add(new BranchBean("01","01","虚拟产品"));
            return data;
        }
    }
}
